package com.feihong.txgw.ui.activity.user;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.feihong.txgw.R;
import com.feihong.txgw.message.LoginMessage;
import com.feihong.txgw.model.bean.user.User;
import com.feihong.txgw.ui.activity.base.BaseActivity;
import com.feihong.txgw.utils.ToastUtils;
import com.feihong.txgw.utils.UserLoginInfo;
import com.feihong.txgw.utils.helper.RxHelper;
import com.feihong.txgw.utils.manage.RetrofitManager;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by PVer on 2017/8/3.
 */

public class UserEditNickName extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_pagetitle)
    TextView txtPagetitle;
    @BindView(R.id.change_nick_name)
    EditText changeNickName;
    @BindView(R.id.btn_user_nick_name)
    Button btnUserNickName;
    private String userName;

    User user = new User();

    @Override
    public void setTheme() {

    }

    @Override
    public int setLayoutID() {
        return R.layout.user_change_nickname;
    }

    @Override
    public void initView(Bundle savedInstanceState) {

        user.setUserPhone(UserLoginInfo.getUserPhone());

        setBackButtonFinish();
        txtPagetitle.setText("用户昵称");
        txtPagetitle.setTextColor(getResources().getColor(R.color.black_text));
    }

    @Override
    public void bindData() {
        changeNickName.setText(UserLoginInfo.getUserInfo().getUserName());
    }

    @Override
    public void onUserLogin(LoginMessage loginMessage) {

    }

    @OnClick({R.id.btn_user_nick_name})
    public void onViewClicked(View view) {

        //昵称保存
//        String REGEX_USERNAME = "^[\\w\\u4e00-\\u9fa5]{1,100}(?<!_)$";

        userName = changeNickName.getText().toString().trim();
//        if (RegexUtils.isMatch(REGEX_USERNAME, userName)) {

        HashMap<String, String> mParams = new HashMap<>();
        mParams.put("type", "3");
        mParams.put("nick_name", userName);
        RetrofitManager.getInstance().editUserNickName(mParams).compose(RxHelper.<String>rxSchedulerHelper())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(String result) {
                        try {
                            if (activityIsDestory()) {
                                return;
                            }
                            user.setUserName(userName);

                            JSONObject json = JSONObject.parseObject(result);

                            //成功
                            if ("200".equals(json.get("code").toString())) {
                                UserLoginInfo.saveUserLoginInfo(user);

                                LoginMessage login = new LoginMessage();
                                login.setAction(LoginMessage.ACTION_LOGIN);
                                login.setUser_type("name");
                                EventBus.getDefault().post(login);
                                ToastUtils.getInstance().showShortToast("信息保存成功!");
                                finish();
                            } else if ("2006".equals(json.get("code").toString())) {
                                ToastUtils.getInstance().showShortToast(json.getString("msg"));
                            } else {
                                ToastUtils.getInstance().showShortToast("信息保存失败,请重试~");
                            }
                        } catch (Exception err) {
                            ToastUtils.getInstance().showShortToast("信息保存失败,请重试~");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                        showDialog(null, getString(R.string.connect_service_error));
                    }

                    @Override
                    public void onComplete() {

                    }
                });
//        App.getApp().getHttpTools().userInfoEdit(3, userName, commonCallback);


//        } else {
//            ToastUtils.showShort("您输入的用户名不符合要求");
//        }

    }
}
